<?php


if (isset($_REQUEST['id'])) {
	$category = $db->fetchRow("SELECT * FROM shop_categories WHERE id=? AND site_id=?", array($_REQUEST['id'], $site['id']));
} else {
	$category = $db->fetchRow("SELECT * FROM shop_categories WHERE parent_id=? AND site_id=?", array(0, $site['id']));
}

if (isset($_POST['category'])) {
	$errors = array();
	if (!$_POST['category']['title']) $errors[] = 'TITLE';
	if (sizeof($errors)) {
		$smarty->assign('errors', $errors);
	} else {
		if (!$_POST['category']['path'] and $category['parent_id'] != 0) $_POST['category']['path'] = make_path($_POST['category']['title']);
		if (!$_POST['category']['image']) $_POST['category']['image'] = make_path($_POST['category']['title']);
		$category = array_merge($category, $_POST['category']);
		$db->update('shop_categories', $category, $db->quoteInto('id=?', $_POST['id']) . ' AND ' . $db->quoteInto('site_id=?', $site['id']));
	}
}


$smarty->assign('category', $category);
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
	header('Content-type: text/xml');
	$smarty->display('shop/categories/index.tpl.taconite');
} else {
	$smarty->display('shop/categories/index.tpl.html');
}